﻿using System;
using System.Threading.Tasks;
using BookSleeve;
using Codeplex.Data;
using Codeplex.Data.SerializeProvider;
using HashMapper.SerializeProvider.Protobuf;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashMapper.Tests
{
    [TestClass]
    public class HashMapperTest
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public async Task GetTest()
        {
            var config = new HashMapperConfig(
                db: 0,
                keyPrefix: "", 
                serializeProviderFactory: () => new ProtobufSerializeProvider());

            using (var conn = new RedisConnection("localhost"))
            {
                await conn.Open();

                // Create mapper, Key is "MyClass:10"
                var mapper = new HashMapper<MyClass>(conn, id: 10, config: config);

                mapper.Key.Is("MyClass:10");

                // Store object
                await mapper.Set(new MyClass { IntProp = 100, StrProp = "Hoge" });

                // Get from Redis
                var value = await mapper.Get();

                // Get field
                var strProp = await mapper.GetField(x => x.StrProp);

                // Atomic increment
                await mapper.Increment(x => x.IntProp, 50);




                var fromDb = await mapper.Get();
                fromDb.StrProp.Is("Hoge");

                var inc = await mapper.Increment(x => x.IntProp, 100);
                inc.Is(250);
                (await mapper.GetField<int>(x => x.IntProp)).Is(250);

                await mapper.Remove();
            }
        }

        public class MyClass
        {
            public int IntProp { get; set; }
            public string StrProp { get; set; }
        }
    }
}
